//
//  CinemaViewController.m
//  UI_TabBarTest1(豆瓣)
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "CinemaViewController.h"
#import "Cinema.h"
#import "CinemaTableViewCell.h"
#import "GetHeightTools.h"
#define kWidth self.view.frame.size.width
@interface CinemaViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)NSMutableArray *cinemaArray;

@end

@implementation CinemaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.cinemaArray = [NSMutableArray arrayWithCapacity:5];
    
    [self setNavi];
    [self handleData];
    [self createTableView];
}

-(void)setNavi{
    self.title = @"影院";
}

-(void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"cinemalist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSDictionary *dictionarySecond = [dictionary valueForKey:@"result"];
    NSArray *array = [dictionarySecond valueForKey:@"data"];
    for (NSDictionary *dic in array){
        Cinema *cinema = [[Cinema alloc] init];
        [cinema setValuesForKeysWithDictionary:dic];
        [self.cinemaArray addObject:cinema];
        [cinema release];
    }
}

-(void)createTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
    [tableView registerClass:[CinemaTableViewCell class] forCellReuseIdentifier:@"cinemaReuse"];
    [tableView release];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cinemaArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160 + [GetHeightTools heightWith:[[self.cinemaArray objectAtIndex:indexPath.row] valueForKey:@"address"] withWidth:kWidth - 20 withFontSize:17.0];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cinemaReuse" forIndexPath:indexPath];
    cell.cinema = [self.cinemaArray objectAtIndex:indexPath.row];
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
